package com.linln.admin.bank.controller;

import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.linln.admin.bank.domain.Clue;
import com.linln.admin.bank.service.ClueService;
import com.linln.admin.bank.validator.ClueForm;
import com.linln.admin.core.enums.ResultEnum;
import com.linln.admin.core.enums.StatusEnum;
import com.linln.admin.core.exception.ResultException;
import com.linln.admin.core.shiro.ShiroUtil;
import com.linln.admin.core.utils.TimoExample;
import com.linln.admin.system.domain.User;
import com.linln.core.utils.FormBeanUtil;
import com.linln.core.utils.ResultVoUtil;
import com.linln.core.vo.ResultVo;

/**
 * 分派完善任务
 *
 * @author kome
 * @date 2020/08/08
 */
@Controller
@RequestMapping(AllotController.BIZ)
public class AllotController {

	static final String BIZ = "/to_allot";
	static final String BIZ_TEMPLATE = "/bank" + BIZ;

	@Autowired
	private ClueService clueService;

	/**
	 * 列表页面
	 *
	 * @param pageIndex 页码
	 * @param pageSize 获取数据长度
	 */
	@GetMapping("/index")
	@RequiresPermissions(AllotController.BIZ + "/index")
	public String index(Model model, Clue clue, @RequestParam(value = "page", defaultValue = "1") int pageIndex,
			@RequestParam(value = "size", defaultValue = "10") int pageSize) {

		// 创建session对象
		Session session = SecurityUtils.getSubject().getSession();
		User user = ShiroUtil.getSubject();
		if (user.getDeptId() != null && user.getDeptId() > 1) {
			session.setAttribute("orgCode", user.getDeptId());// 把用户数据保存到session对象中
			clue.setOrgCode(user.getDeptId().toString());
		}

		// 创建匹配器，进行动态查询匹配
		ExampleMatcher matcher = ExampleMatcher.matching();
		clue.setAllotStatus((byte) 0);

		// 获取业务管理列表
		Example<Clue> example = TimoExample.of(clue, matcher);
		Page<Clue> list = this.clueService.getPageList(example, pageIndex, pageSize);

		// 封装数据
		model.addAttribute("list", list.getContent());
		model.addAttribute("page", list);
		return BIZ_TEMPLATE + "/index";
	}

	/**
	 * 跳转到添加页面
	 *
	 * @param model
	 */
	@GetMapping("/add")
	@RequiresPermissions(AllotController.BIZ + "/add")
	public String toAdd(Model model) {
		model.addAttribute("sessionUser", ShiroUtil.getSubject());
		return BIZ_TEMPLATE + "/add";
	}

	/**
	 * 跳转到编辑页面
	 */
	@GetMapping("/edit/{id}")
	@RequiresPermissions(AllotController.BIZ + "/edit")
	public String toEdit(@PathVariable("id") Long id, Model model) {
		Clue clue = this.clueService.getId(id);

		model.addAttribute("clue", clue);
		return toAdd(model);
	}

	/**
	 * 保存添加/修改的数据
	 *
	 * @param clueForm 表单验证对象
	 */
	@PostMapping({ "/add", "/edit" })
	@RequiresPermissions({ AllotController.BIZ + "/add", AllotController.BIZ + "/edit" })
	@ResponseBody
	public ResultVo save(@Validated ClueForm clueForm) {

		// 将验证的数据复制给实体类
		Clue clue = new Clue();
		if (clueForm.getId() != null) {
			Clue exists = this.clueService.getId(clueForm.getId());
			if (exists != null) {
				clue = exists;
			}
		}
		FormBeanUtil.copyProperties(clueForm, clue);

		// 保存数据
		this.clueService.save(clue);
		return ResultVoUtil.SAVE_SUCCESS;
	}

	/**
	 * 跳转到详细页面
	 */
	@GetMapping("/detail/{id}")
	@RequiresPermissions(AllotController.BIZ + "/detail")
	public String toDetail(@PathVariable("id") Long id, Model model) {
		Clue clue = this.clueService.getId(id);
		model.addAttribute("clue", clue);
		return BIZ_TEMPLATE + "/detail";
	}

	/**
	 * 设置一条或者多条数据的状态
	 */
	@RequestMapping("/status/{param}")
	@RequiresPermissions(AllotController.BIZ + "/status")
	@ResponseBody
	public ResultVo status(@PathVariable("param") String param, @RequestParam(value = "ids", required = false) List<Long> idList) {
		try {
			// 获取状态StatusEnum对象
			StatusEnum statusEnum = StatusEnum.valueOf(param.toUpperCase());
			// 更新状态
			Integer count = this.clueService.updateStatus(statusEnum, idList);
			if (count > 0) {
				return ResultVoUtil.success(statusEnum.getMessage() + "成功");
			} else {
				return ResultVoUtil.error(statusEnum.getMessage() + "失败，请重新操作");
			}
		} catch (IllegalArgumentException e) {
			throw new ResultException(ResultEnum.STATUS_ERROR);
		}
	}

}
